//引入jquery
//document.write('<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>');
var currentPage = 2;
var isLoading = false;
var rowOneCtn = document.getElementById("row1");
var rowTwoCtn = document.getElementById("row2");
var __width = window.innerWidth;
var __height = __width / 2 - 15;
var lastLoadTime = Date.parse(new Date());
var noMoreContent=false;

$("body").append('<script src="/js/config/config.js"></script>');

var firstLoadedImgFlage = false;
var defaultIcon = new Image();
function loadImage(url, i) {
	var img = new Image(); //创建一个Image对象，实现图片的预下载
	img.src = url;
	img.className = "m-icon";
	if(img.complete) { // 如果图片已经存在于浏览器缓存，直接调用回调函数
		var linkId = document.getElementById("linkId-" + i);
		linkId.innerHTML = "";
		linkId.appendChild(img);
		if(!firstLoadedImgFlage) {
			defaultIcon.src = url;
			defaultIcon.className = "m-icon";
			firstLoadedImgFlage = true;
		}
		return; // 直接返回，不用再处理onload事件
	}
	img.onload = function() { //图片下载
		var linkId = document.getElementById("linkId-" + i);
		linkId.innerHTML = "";
		linkId.appendChild(img);
		if(!firstLoadedImgFlage) {
			defaultIcon.src = url;
			defaultIcon.className = "m-icon";
			firstLoadedImgFlage = true;
		}
	}
	img.onerror = function() {
		reloadImage(url, i);
	}
}

function reloadImage(url, i) {
	var img = new Image(); //创建一个Image对象，实现图片的预下载
	img.src = url;
	img.className = "m-icon";
	if(img.complete) { // 如果图片已经存在于浏览器缓存，直接调用回调函数
		var linkId = document.getElementById("linkId-" + i);
		linkId.innerHTML = "";
		linkId.appendChild(img);
		return; // 直接返回，不用再处理onload事件
	}
	img.onload = function() { //图片下载
		var linkId = document.getElementById("linkId-" + i);
		linkId.innerHTML = "";
		linkId.appendChild(img);
	}
	img.onerror = function() {
		console.log("Image load error:"+url);
		loadDefaultIcon(i);
	}
}

function loadDefaultIcon(i) {
	var linkId = document.getElementById("linkId-" + i);
	linkId.innerHTML = "";
	linkId.appendChild(defaultIcon);
}

function lowEnough(){
	var doc_height = $(document).height();
    var scroll_top = $(document).scrollTop(); 
    var window_height = $(window).height();
    return doc_height-scroll_top-window_height<50 ;
}
function loadMore(){
	$.ajax({  
        url: '/api/books/getMore/'+currentPage,  
        type: 'GET',  
        dataType: 'json',  
        timeout: 5000,  
        cache: false,  
        beforeSend: LoadFunction, 
        error: erryFunction,  
        success: addContent
    });
}
function LoadFunction() {
}  
function erryFunction() {  
    //alert("error");  
}
function addContent(resultJson) {
	var json = eval(resultJson);
    var code = json.code;
	if(code==200){
		var recordList = json.data;
		if(recordList==null || recordList.length==0){
			noMoreContent = true;
    		return;
    	}
		currentPage++;
		$.each(recordList, function (index) {
			var currentRecord = recordList[index];
			var colorR = Math.floor(Math.random() * 256);
			var colorG = Math.floor(Math.random() * 256);
			var colorB = Math.floor(Math.random() * 256);

			if(index % 2 == 0) {
				rowOneCtn.innerHTML += '<div id="index-li-' + currentRecord.id + '"  class="gameCtn" >' +
					'<a href="/books/detail/'+currentRecord.id+'"  id="linkId-' + currentRecord.id + '" >'+
					'<div class="previewDiv" id="previewDiv-' + currentRecord.id + '" style="height:' + __height + 'px;background-color:rgb(' + colorR + ',' + colorG + ',' + colorB + ');" ></div>'+
					'</a>' +
					'<div class="gametitle" ><p >' + currentRecord.name + '</p></div>' +
					'</div>';

			} else {
				rowTwoCtn.innerHTML += '<div id="index-li-' + currentRecord.id + '" class="gameCtn" >' +
					'<a href="/books/detail/'+currentRecord.id+'" id="linkId-' + currentRecord.id + '" >'+
					'<div class="previewDiv" id="previewDiv-' + currentRecord.id + '" style="height:' + __height + 'px;background-color:rgb(' + colorR + ',' + colorG + ',' + colorB + ');" ></div>'+
					'</a>' +
					'<div class="gametitle" ><p >' + currentRecord.name + '</p></div>' +
					'</div>';
			}
		});
		$.each(recordList, function (index) {
			var currentRecord = recordList[index];
			loadImage(imgUrl+currentRecord.cover, currentRecord.id);
		});
	}
}
//第一次加载内容
loadMore();
//滚动加载内容
window.onscroll = function() {
	if(noMoreContent){
		return;
	}
	if(lowEnough()){
		var nowTimestamp = Date.parse(new Date());
		var timeGap = nowTimestamp-lastLoadTime;
		$("#loading-warp-end-id").css({"display":""});
		if(isLoading==false && timeGap>=1000){
			isLoading=true;
			lastLoadTime = Date.parse(new Date());
			loadMore();
			isLoading=false;
			$("#loading-warp-end-id").css({"display":"none"});
		}
	}
};